package org.answer.controller;


import org.answer.constant.MessageConstant;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.entity.Result;
import org.answer.pojo.Role;
import org.answer.pojo.User;
import org.answer.service.RoleService;
import org.answer.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/checkuser")
public class CheckUserController {

    @Reference
    private UserService userService;

    @Reference
    private RoleService roleService;


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody User user,Integer[] checkRoleIds){


        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.add(user,checkRoleIds);


        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_User_FAIL);
        }
        return new Result(true,MessageConstant.ADD_User_SUCCESS);
    }

    @RequestMapping("/findRoleAll")
    public Result findRoleAll(){

        try {
            List<Role> roleAll = roleService.findRoleAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleAll);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            List<Integer> rolesid = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_User_SUCCESS,rolesid);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_User_FAIL);
        }
    }



    @RequestMapping("/delete")
    public Result AllDelete(Integer id){
        try {
            userService.AllDelete(id);
            return new Result(true,MessageConstant.DELETE_User_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_User_FAIL);
        }
    }

    @RequestMapping("/findUserByid")
    public Result findUserByid(Integer id){
        try {
            User userByid = userService.findUserByid(id);
            return new Result(true,MessageConstant.QUERY_User_SUCCESS,userByid);
        }catch (Exception e){
            return new Result(true,MessageConstant.QUERY_User_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] checkRoleIds){

        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.edit(user);

            Integer id = user.getId();
            //删除关联关系
            userService.delete(id);
            //插入关联关系
            userService.insert(id,checkRoleIds);


        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_User_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_User_SUCCESS);
    }

}
